package com.hz.web.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hz.web.entity.RoleEntity;
import com.hz.web.entity.UserEntity;
import com.hz.web.service.IroleMenuService;
import com.hz.web.service.IroleService;
import com.hz.web.service.IuserService;

@Controller
@RequestMapping("role")
public class RoleController {

	@Resource
	private IroleService roleService;
	
	@Resource
	private IuserService userService;
	
	@Resource
	private IroleMenuService roleMenuService;
	
	
	/**
	 * 删除权限信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("delRole")
	@ResponseBody
	public Map<String, Object> delRole(RoleEntity entity){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		int roleId = entity.getRoleId();
		int userRow = roleMenuService.queryRoleMenuCount(roleId);//这个权限有没有关联菜单
		
		int delrow = -1;
		try {
			UserEntity user = new UserEntity();
			user.setRoleId(entity.getRoleId());
			user.setRoleName("无权限");
			userService.updateUserByRoleId(user);
			if(userRow==0){
				delrow = roleService.delOnlyRole(entity);
			}else{
				delrow =roleService.delRole(entity);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("msg", "删除权限异常！");
			resultMap.put("row", delrow);
		}
		if(delrow>=0){
			resultMap.put("msg", "删除权限成功！");
		}else{
			resultMap.put("msg", "删除权限失败！");
		}
		
		resultMap.put("row", delrow);
		return resultMap;
	}
	
	/**
	 * 修改权限信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("updateRole")
	@ResponseBody
	public Map<String, Object> updateRole(RoleEntity entity){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		parmsMap.put("roleId",entity.getRoleId());
		int userRow = userService.queryUserCountByRoleId(parmsMap);
		
		int row = -1;
		try {
			if(userRow==0){
				row = roleService.updateOnlyRole(entity);//只修改权限信息
			}else{
				row = roleService.updateRole(entity);//修改权限信息，同时修改用户信息
			}
			
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("msg", "修改信息异常！");
		}
		if(row!=-1){
			resultMap.put("msg", "修改权限成功！");
		}
		resultMap.put("row", row);
		return resultMap;
	}
	
	/**
	 * 添加权限信息
	 * @param entity
	 * @return
	 */
	@RequestMapping("insertRole")
	@ResponseBody
	public Map<String, Object> insertRole (RoleEntity entity){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		int row = -1;
		try {
			row = roleService.insertRole(entity);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("msg", "添加信息异常！");
		}
		if(row!=-1){
			resultMap.put("msg", "添加权限成功！");
		}
		resultMap.put("row", row);
		return resultMap;
	}
	
	/**
	 * 查询所有的权限角色(提供给角色管理里面的  分配权限使用)
	 * @return
	 */
	@RequestMapping("queryRoleList")
	@ResponseBody
	public List<RoleEntity> queryRoleList(){
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		List<RoleEntity> list = new ArrayList<RoleEntity>();
		try {
			list=roleService.queryRole(parmsMap);
		} catch (Exception e) {
			e.printStackTrace();
			return list;
		}
		return list;
		
	}
	
	/**
	 * 查询所有的权限角色
	 * @return
	 */
	@RequestMapping("queryRole")
	@ResponseBody
	public Map<String, Object> queryRole(){
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> parmsMap = new HashMap<String, Object>();
		
		
		List<RoleEntity> ls = roleService.queryRole(parmsMap);
		resultMap.put("rows", ls);
		return resultMap;
		
	}
	
	
}
